package com.ebai.product.management.service.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ebai.product.management.base.cache.service.PublicCacheImpl;
import com.ebai.product.management.model.vojo.RoleAuthVo;
import com.ebai.product.management.repository.system.AuthRepository;

import net.sf.json.JSONArray;

/**
 * 角色权限业务处理类
 * @author mgli
 *
 */
@Service
public class RoleAuthService {
	
	@Autowired
	private AuthRepository authRepository;

	public Map<String, Object> queryAuthsAll(String rolePid) {
		Map<String, Object> result = new HashMap<String, Object>();
        result.put("is", false);
        
        try {
        	List<Object> objAuths = this.authRepository.selectAuthsTreeTable(rolePid);
    		List<RoleAuthVo> listAuthVos = new ArrayList<RoleAuthVo>();
    		RoleAuthVo roleAuthVo = null;
    		if (null != objAuths && objAuths.size()>0) {
    			for (Object authObj : objAuths) {
    				roleAuthVo = new RoleAuthVo();
    				Object[] authArr = (Object[]) authObj;
//    				roleAuthVo.setId((String) authArr[0]);
//    				roleAuthVo.setPid((String) authArr[1]);
//    				roleAuthVo.setTitle((String) authArr[2]);
    				roleAuthVo.setId((String) authArr[0]);	
    				roleAuthVo.setpId((String) authArr[1]);
    				roleAuthVo.setName((String) authArr[2]);
    				roleAuthVo.setAuthType((String) authArr[3]);
    				roleAuthVo.setAuthPerms((String) authArr[4]);
    				roleAuthVo.setDirectUrl((String) authArr[5]);
    				roleAuthVo.setSortOrder((String) authArr[6]);
    				if (StringUtils.equals((String) authArr[3], "MENU")) {
    					roleAuthVo.setLay_is_open(true);
    				}
    				if (StringUtils.isNotEmpty((String) authArr[7])) {
    					roleAuthVo.setLay_is_checked(true);
    				}
    				roleAuthVo.setAuthTypeText(PublicCacheImpl.showDictText("QXLX", (String) authArr[3]));
    				
    				listAuthVos.add(roleAuthVo);
    			}
    		}
    		JSONArray jsonArray = JSONArray.fromObject(listAuthVos);
    		System.out.println(jsonArray.toString());
    		
    		result.put("code", "0");
    		result.put("count", listAuthVos.size());
    		result.put("data", jsonArray);
    		result.put("is", true);
    		result.put("msg", "");
    		result.put("tip", "获取数据成功");
		} catch (Exception e) {
			// TODO: handle exception
    		result.put("msg", "获取数据失败");
			e.printStackTrace();
		}
        
		return result;
	}
}
